package cn.ingrun.cloud_disk.model;

import cn.ingrun.cloud_disk.utils.StringUtil;
import org.springframework.util.DigestUtils;

public class User {

    private int id;
    private String username;
    private String password;
    private String email;
    private String phone_number;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", phone_number='" + phone_number + '\'' +
                '}';
    }

    public User() {
    }

    public User(int id, String username, String password, String email, String phone_number) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.phone_number = phone_number;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setMD5Password(){
        this.password = DigestUtils.md5DigestAsHex(this.getPassword().getBytes());
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }


    public static Boolean isPhone(String id){
        return id.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$");
    }

    public static Boolean isEmail(String id){
        String regEx1 = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        return id.matches(regEx1);
    }

    // 检测用户信息是否合法， 是否有空信息
    public ResponseJson checkUser(){
        this.setEmptyForNull();

        if(StringUtil.isNullOrEmpty(this.username)){
            return new ResponseJson(0, "用户名不存在！", this);
        }
        if (StringUtil.isNullOrEmpty(this.password)){
            return new ResponseJson(0, "密码不存在！", this);
        }

        if (this.emailOrPhone_number().getCode() == 0){
            return new ResponseJson(0, "手机号或邮箱至少存在一个",this);
        }
        if (!this.email.equals("")){
            if (!User.isEmail(this.email)){
                return new ResponseJson(0,"邮箱格式不正确！",this);
            }
        }
        if (!this.phone_number.equals("")){
            if (!User.isPhone(this.getPhone_number())){
                return new ResponseJson(0, "手机号不正确！",this);
            }
        }

        return new ResponseJson(1,"","");
    }

    //检测手机号，邮箱存在关系
    //0,1,2,3  不存在，邮箱存在，手机号存在，都存在
    public ResponseJson emailOrPhone_number(){
        int code = 0;    //0,1,2,3  不存在，邮箱存在，手机号存在，都存在
        if (!StringUtil.isNullOrEmpty(this.email)){
            code += 1;
        }
        if (!StringUtil.isNullOrEmpty(this.phone_number)){
            code += 2;
        }
        return new ResponseJson(code, "", "");
    }

    // 将空字段设置为空字符串
    public User setEmptyForNull(){
        if (this.username == null){
            this.username = "";
        }
        if (this.password == null){
            this.password = "";
        }
        if (this.email == null){
            this.email = "";
        }
        if (this.phone_number == null){
            this.phone_number = "";
        }
        return this;
    }

    //检测密码是否正确
    public Boolean checkPassword(String password){
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        return md5password.equals(this.password);
    }
}
